package com.wisemoresystem.seal.service;

import java.util.List;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_bank_level;
import com.wisemoresystem.seal.model.t_seal_new;
import com.wisemoresystem.seal.model.t_seal_type_config;
import com.wisemoresystem.seal.model.t_seal_workflow;
import com.wisemoresystem.seal.model.t_seal_workflow_module;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 工作流流程
 * 
 * @author Administrator
 *
 */
@Service
public class WorkflowService extends BaseService {

	@Autowired
	private BankLevelService levelService;
	@Autowired
	private BankService bService;

	// 申请审核 批复流转单
	@Transactional
	public void wkflowApply(t_seal_new seal_new) {
		t_seal_workflow wk = new t_seal_workflow();

		wk.setType("新章审批");
		wk.setFlag("进行中");
		wk.setUser_id(WebTools.getCurrentUserId());

		t_bank bank = super.findObjById(seal_new.getBank_id(), t_bank.class);
		int start_lev = bService.getBankLevel(bank.get_id())-1;

		t_seal_type_config stc = super.findObjById(
				seal_new.getSeal_type_config_id(), t_seal_type_config.class);
		t_bank_level endLevel = levelService
				.getLevelByName(stc.getVerify_new());
		
		ht.save(wk);

		seal_new.setWorkflow_id(wk.get_id());
		
		ht.update(seal_new);
		
		if(start_lev<endLevel.getSort()){
			t_seal_workflow_module module = new t_seal_workflow_module();
			module.setWorkflow_id(wk.get_id());
			module.setFlag("未启动");
			module.setSort(0);
			module.setVerify_bank_id(bank.get_id());
			module.setFlag("待审批");

			ht.save(module);
			return ;
		}

		// 查询审批流程银行
		List<t_bank_level> list = levelService.selectBankLevel(
				start_lev, endLevel.getSort());
		t_bank verifyBank = super.findObjById(bank.getSuper_id(), t_bank.class);
		
		for (int i = 0; i < list.size(); i++) {

			t_seal_workflow_module module = new t_seal_workflow_module();
			module.setWorkflow_id(wk.get_id());
			module.setFlag("未启动");
			module.setSort(i);
			if(ChkTools.isNotNull(verifyBank))
				module.setVerify_bank_id(verifyBank.get_id());

			if (i == 0) {
				module.setFlag("待审批");
			}

			ht.save(module);
			if(ChkTools.isNotNull(verifyBank))
				verifyBank = super.findObjById(verifyBank.getSuper_id(),
					t_bank.class);

		}

	}// #wkflowApply

	// 查询待审批的新章列表
	public void selectSealNewToVerifyList(Page page) {
		
		
	}
}
